package com.maxatime.job;

import com.maxatime.utils.MailService;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.text.SimpleDateFormat;
import java.util.Date;

//@Configuration
public class QuarteConfig {

    @Autowired
    private MailService emailUtil;
//    @Bean
//    public JobDetail jobDetail1(){
//        return JobBuilder.newJob(QuarteText01.class).storeDurably().build();
//    }

//    @Bean
//    public Trigger trigger1(){
//        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
//                .withIntervalInSeconds(5) //每几秒秒执行一次
//                .repeatForever(); //永久重复，一直执行下去
//
//        return TriggerBuilder.newTrigger()
//                .forJob(jobDetail1())
//                .withSchedule(scheduleBuilder)
//                .build();
//    }
  //  @Bean
    public JobDetail jobDetail2(){
        QuartzJobBean quartzJob2 = new QuartzJobBean() {
            @Override
            protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                System.out.println("内部类quartzJob2----" + sdf.format(new Date()));
                try {
                    emailUtil.sendSimpleMail("zslsl521@163.com", "我", "我是定时" + sdf.format(new Date()));
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        };
        return JobBuilder.newJob(quartzJob2.getClass()).storeDurably().build();
    }

  //  @Bean
    public Trigger trigger2(){
        //JobDetail的bean注入不能省略
        //JobDetail jobDetail3 = JobBuilder.newJob(QuartzJob2.class).storeDurably().build();
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInHours(12)//每12小时执行一次
//                 .withIntervalInSeconds(30)
                .repeatForever(); //永久重复，一直执行下去
        return TriggerBuilder.newTrigger()
                .forJob(jobDetail2())
                .withSchedule(scheduleBuilder).build();
    }
}
